package com.zwy.space.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zwy.space.domain.Blog;
import com.zwy.space.queryVo.IndexVo;
import com.zwy.space.queryVo.RecommendVo;
import com.zwy.space.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private BlogService blogService;

    @RequestMapping({"/","index"})
    public String getIndexPage(Model model, @RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum){

        PageHelper.startPage(pageNum,4);
        List<IndexVo> blogs=blogService.findBlogsByPage(pageNum);
        List<RecommendVo> recommendBlogs=blogService.findRecommendBlogs();

        PageInfo<IndexVo> pageInfo = new PageInfo<>(blogs);

//        System.out.println(pageInfo);

        model.addAttribute("blogs",pageInfo);
        model.addAttribute("recommendBlogs",recommendBlogs);
        return "index";
    }

    @PostMapping("/search")
    public String search(Model model,
                         @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                         @RequestParam String query){
        PageHelper.startPage(pageNum,4);
        List<IndexVo> blogs=blogService.searchBlogs(query);
        System.out.println(blogs);

        PageInfo<IndexVo> pageInfo = new PageInfo<>(blogs);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("query", query);


        return "search";
    }

}
